package cn.edu.sgu.www.mhxysy.service.xingyin.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.xingyin.XingyinTexiaoCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.xingyin.XingyinTexiaoCategory;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.XingyinTexiaoCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.xingyin.XingyinTexiaoCategoryPager;
import cn.edu.sgu.www.mhxysy.service.xingyin.XingyinTexiaoCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class XingyinTexiaoCategoryServiceImpl implements XingyinTexiaoCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final XingyinTexiaoCategoryMapper xingyinTexiaoCategoryMapper;

    @Autowired
    public XingyinTexiaoCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            XingyinTexiaoCategoryMapper xingyinTexiaoCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.xingyinTexiaoCategoryMapper = xingyinTexiaoCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_XINGYIN_TEXIAO;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(XingyinTexiaoCategoryDTO xingyinTexiaoCategoryDTO) {
        XingyinTexiaoCategory xingyinTexiaoCategory = toEntity(xingyinTexiaoCategoryDTO);

        xingyinTexiaoCategory.setId(null);

        xingyinTexiaoCategoryMapper.insert(xingyinTexiaoCategory);
    }

    @Override
    public void deleteById(Integer id) {
        xingyinTexiaoCategoryMapper.deleteById(id);
    }

    @Override
    public void updateById(XingyinTexiaoCategoryDTO xingyinTexiaoCategoryDTO) {
        ObjectUtils.requireNonNull(xingyinTexiaoCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        XingyinTexiaoCategory xingyinTexiaoCategory = toEntity(xingyinTexiaoCategoryDTO);

        xingyinTexiaoCategoryMapper.updateById(xingyinTexiaoCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<XingyinTexiaoCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                xingyinTexiaoCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public List<XingyinTexiaoCategory> selectAll() {
        QueryWrapper<XingyinTexiaoCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByAsc("type");

        return xingyinTexiaoCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public XingyinTexiaoCategory selectById(Integer id) {
        return xingyinTexiaoCategoryMapper.selectById(id);
    }

    @Override
    public Page<XingyinTexiaoCategory> selectByPage(XingyinTexiaoCategoryPager pager) {
        QueryWrapper<XingyinTexiaoCategory> queryWrapper = new QueryWrapper<>();
        Page<XingyinTexiaoCategory> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );

        return xingyinTexiaoCategoryMapper.selectPage(page, queryWrapper);
    }

    private XingyinTexiaoCategory toEntity(XingyinTexiaoCategoryDTO xingyinTexiaoCategoryDTO) {
        XingyinTexiaoCategory xingyinTexiaoCategory = new XingyinTexiaoCategory();

        xingyinTexiaoCategory.setId(xingyinTexiaoCategoryDTO.getId());
        xingyinTexiaoCategory.setName(xingyinTexiaoCategoryDTO.getName());
        xingyinTexiaoCategory.setType(xingyinTexiaoCategoryDTO.getType());
        xingyinTexiaoCategory.setNote(xingyinTexiaoCategoryDTO.getNote());
        xingyinTexiaoCategory.setScore(xingyinTexiaoCategoryDTO.getScore());

        return xingyinTexiaoCategory;
    }

}